package com.shj.em.biz.hr.controller;

import com.shj.em.base.controller.BaseController;
import com.shj.em.base.exception.BizException;
import com.shj.em.biz.hr.service.DeptService;
import com.shj.em.biz.hr.service.EmpService;
import com.shj.em.biz.hr.vo.EmpInitVO;
import com.shj.em.biz.hr.vo.EmpSearchDTO;
import com.shj.em.biz.sys.entity.Cd;
import com.shj.em.biz.hr.entity.Dept;
import com.shj.em.biz.sys.entity.DeptEmp;
import com.shj.em.biz.sys.service.*;
import org.springframework.stereotype.Controller;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Arrays;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import com.shj.em.common.annotation.Log;
import com.shj.em.base.support.ResMsg;
import com.shj.em.base.vo.PageVO;

import com.shj.em.biz.hr.entity.Emp;
import com.baomidou.mybatisplus.plugins.Page;
import lombok.extern.slf4j.Slf4j;


/**
 * <p>
 * 员工信息 前端控制器
 * </p>
 *
 * @author Shen Huang Jian
 * @since 2018-07-03
 */
@RestController
@Slf4j
@RequestMapping("/hr/emp")
public class EmpController extends BaseController<Emp> {

    @Autowired
    EmpService empService;

    @Autowired
    DeptService deptService;

    @Autowired
    DeptEmpService deptEmpService;

    @GetMapping("/init")
    public ResMsg init(){
        List<Cd> sexList = cdService.queryCdsByType(CdService.CD_TYPE_SEX);
        EmpInitVO initVO = new EmpInitVO();
        initVO.setSexList(sexList);
        return ResMsg.ok(initVO);
    }

    @GetMapping("/list")
    public PageVO<Emp> list(EmpSearchDTO searchDTO){
        Page<Emp> page = empService.queryEmpPage(searchDTO);
        PageVO<Emp> pageVO = new PageVO<>();
        pageVO.setTotal(page.getTotal());
        pageVO.setRows(page.getRecords());
        return pageVO;
    }

    @GetMapping("/{id}")
    public ResMsg<Emp> gotoEdit(@PathVariable Long id){
        return ResMsg.ok(empService.queryEmpDetailById(id));
    }

    @Log("创建员工信息")
    @PostMapping
    public ResMsg add(@RequestBody Emp emp){
        boolean flag = empService.isUserIdExist(emp.getUserId(), null);
        if(flag){
            throw new BizException("用户名已存在。请换一个试试。");
        }
        empService.insert(emp);
        return ResMsg.ok("成功创建员工信息");
    }

    @Log("更新员工信息")
    @PutMapping
    public ResMsg edit(@RequestBody Emp emp){
        boolean flag = empService.isUserIdExist(emp.getUserId(), emp.getId());
        if(flag){
            throw new BizException("用户名已存在。请换一个试试。");
        }
        empService.updateById(emp);
        return ResMsg.ok("成功更新员工信息");
    }

    /*@GetMapping("/gotoAdd")
	String gotoAdd(Model model){
        Emp emp = new Emp();
        emp.setSex("M"); //设置默认选中
        emp.setChangeDt(new Date());
        model.addAttribute("isEdit", false);
        model.addAttribute("emp", emp);

        List<Cd> sexList = cdService.queryCdsByType(CdService.CD_TYPE_SEX);
        model.addAttribute("sexList", sexList);
        model.addAttribute("dept", new Dept());
	    return prefix + "/add";
	}


    @Log("删除员工")
	@PostMapping( "/remove")
	@ResponseBody
	public ResMsg remove(@RequestParam("id") Long id){
        try{
            empService.deleteById(id);
            return ResMsg.ok();
        }catch(Exception e){
            return ResMsg.error();
        }
	}

    @Log("批量删除员工")
	@PostMapping( "/batchRemove")
	@ResponseBody
	public ResMsg batchRemove(@RequestParam("ids[]") Long[] ids){
        empService.deleteBatchIds(Arrays.asList(ids));
		return ResMsg.ok();
	}*/

}

